package edu.scu.declaresys.web.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import edu.scu.declaresys.config.IAttrName;
import edu.scu.declaresys.service.ServiceFactory;
import edu.scu.declaresys.service.intr.IProjectClassService;
import edu.scu.declaresys.vo.ProjectClass;
import edu.scu.declaresys.web.ABaseAction;

/**
 * 
 * @author: zt
 * @since : 2013-7-10
 */
public class ProjectClassAction extends ABaseAction implements ModelDriven<ProjectClass>
{
	private static final long	serialVersionUID	= 1177588117429526087L;
	
	private IProjectClassService service = ServiceFactory.createProjectClassService();
	
	private ProjectClass vo = new ProjectClass();
	
	//
	// actoin
	//
	public String projectClassAdd() throws Exception
	{
		if(vo.getName() == null)
		{
			throw new Exception("类别名称不能为空");
		}
		if(service.findClassByName(vo.getName()) != null)
		{
			throw new Exception("类别已经存在");
		}
		vo.setId(service.addClass(vo));
		request.setAttribute(IAttrName.CLASS_VO, vo);
		return Action.SUCCESS;
	}
	
	public String projectClassDelete() throws Exception
	{
		if(vo.getId() == null)
		{
			throw new Exception("未指定ID");
		}
		else
		{
			//TODO:检查是否存在项目
			service.deleteClass(vo.getId());
		}
		return Action.SUCCESS;
	}
	
	public String projectClassModify() throws Exception
	{
		if(vo.getName() != null)
		{
			if(vo.getId() == null)
			{
				throw new Exception("未指定类别或类类别名未填");
			}
			else if(service.findClassByName(vo.getName()) != null)
			{
				throw new Exception("该类别已经存在！");
			}
			else if(service.findClass(vo.getId()) == null)
			{
				throw new Exception("该类别不存在");
			}
			service.modifyClass(vo);
		}
		return Action.SUCCESS;
	}
	
	public String projectClassList() throws Exception
	{
		request.setAttribute(IAttrName.CLASS_LIST, service.findClassList());
		return Action.SUCCESS;
	}
	
	public String projectClassDetail() throws Exception
	{
		if(vo.getId() != null)
		{
			throw new Exception("未指定类别！");
		}
		vo = service.findClass(vo.getId());
		request.setAttribute(IAttrName.CLASS_VO, vo);
		return Action.SUCCESS;
	}
	
	public String projectClassCheckDuplicate() throws Exception
	{
		boolean isDuplicate = (service.findClassByName(vo.getName()) != null);
		request.setAttribute(IAttrName.CLASS_DUPLICATE_FLAG, isDuplicate);
		return Action.SUCCESS;
	}

	
	//
	// Getters and Setters.
	//
	
	@Override
	public ProjectClass getModel()
	{
		return vo;
	}

	public ProjectClass getProjectClass()
	{
		return vo;
	}

	public void setProjectClass(ProjectClass projectClass)
	{
		this.vo = projectClass;
	}
	
}
